<!doctype html>
<html lang="en">
	<head>
		<title>Tween.js / relative start time</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<div id="info">
			<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
			<h2>13 _ relative start time</h2>
			<br />
			<p>One starts late, Two starts early.</p>
		</div>
		<div
			id="target1"
			style="
				position: absolute;
				transform: translate(100px, 100px);
				width: 100px;
				height: 100px;
				background: #a0dde9;
				padding: 1em;
			"
		>
			One
		</div>

		<div
			id="target2"
			style="
				position: absolute;
				transform: translate(100px, 300px);
				width: 100px;
				height: 100px;
				background: #a0dde9;
				padding: 1em;
			"
		>
			Two
		</div>

		<script type="module">
			import * as TWEEN from '../dist/tween.esm.js'

			const position1 = {x: 100, y: 100, rotation: 0}
			const position2 = {x: 100, y: 300, rotation: 0}
			const target1 = document.getElementById('target1')
			const target2 = document.getElementById('target2')

			const tween1 = new TWEEN.Tween(position1)
				.to({x: 700, y: 200, rotation: 359}, 2000)
				.easing(TWEEN.Easing.Elastic.InOut)
				.onUpdate(update1)

			const tween2 = new TWEEN.Tween(position2).to({x: 500, y: 300, rotation: -90}, 2000).onUpdate(update2)

			tween1.delay(2000).start()
			tween2.delay(100).start()

			animate()

			function animate(time) {
				requestAnimationFrame(animate)

				tween1.update(time)
				tween2.update(time)
			}

			function update1() {
				target1.style.transform = `translate(${position1.x}px, ${position1.y}px) rotate(${Math.floor(
					position1.rotation,
				)}deg)`
			}

			function update2() {
				target2.style.transform = `translate(${position2.x}px, ${position2.y}px) rotate(${Math.floor(
					position2.rotation,
				)}deg)`
			}
		</script>
	</body>
</html>
